using System;
using System.Collections.Generic;
using System.Text;

namespace Decav.Reality
{
    /// <summary>
    /// The code that defines a Country on the globe.
    /// </summary>
    [Serializable]
    public struct Country
    {
        private Country(string code)
        {
            _code = code;
        }

        private string _code;

        /// <summary>
        /// The empty value of a <see cref="Country"/>.
        /// </summary>
        public static readonly Country Empty = new Country();

        /// <summary>
        /// Gets a <see cref="Country"/> from a three letter ISO standard code.
        /// </summary>
        /// <param name="code">The country code.</param>
        /// <returns>The country code.</returns>
        public static Country FromCountryCode(string code)
        {
            if (code == null)
                return Country.Empty;
        
            code = code.ToUpper();
            if (code == "US")
                code = "USA";


            if (code.Length != 3)
                throw new ArgumentException("The code specified is not valid because it is not 3 letters in length.");

            return new Country(code);
        }

        public override int GetHashCode()
        {
            return _code.GetHashCode();
        }

        /// <summary>
        /// Gets a string representation of the country.
        /// </summary>
        /// <returns>The string representation of the country.</returns>
        public override string ToString()
        {
            return (_code == null) ? string.Empty : _code;
        }

        public override bool Equals(object obj)
        {
            if (_code == null && obj == null)
                return true;

            if (_code == null)
                return false;

            return _code.Equals(obj);
        }

        public static bool operator ==(Country left, Country right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(Country left, Country right)
        {
            return !left.Equals(right);
        }
    }
}
